
ARG ALPINE_VERSION=3.11.6
ARG NGINX_VERSION=1.17.10
ARG NGINX_RTMP_VERSION=1.2.1
ARG NJS_VERSION=0.4.1
ARG FFMPEG_VERSION=4.2.3
ARG REPO_MIRRORS_URL=https://mirrors.huaweicloud.com/alpine

FROM alpine:$ALPINE_VERSION as build

ARG ALPINE_VERSION
ARG REPO_MIRRORS_URL
ARG NGINX_VERSION
ARG NGINX_RTMP_VERSION
ARG NJS_VERSION
ARG FFMPEG_VERSION

# ADD nginx-$NGINX_VERSION.tar.gz v$NGINX_RTMP_VERSION.tar.gz $NJS_VERSION.tar.gz ffmpeg-$FFMPEG_VERSION.tar.gz /tmp/

RUN printf "${REPO_MIRRORS_URL}/%s\n" \
    v${ALPINE_VERSION%.*}/main \
    v${ALPINE_VERSION%.*}/community \
    > /etc/apk/repositories && \
    apk update --no-cache; \
:                \
:   install apk  \
;                \
    apk add --no-cache --virtual .download curl && \
    cd /tmp && \
    curl --location --retry 3 http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz | tar -xzf - && \
    curl --location --retry 3 https://github.com/arut/nginx-rtmp-module/archive/v$NGINX_RTMP_VERSION.tar.gz | tar -xzf - && \
    curl --location --retry 3 https://github.com/nginx/njs/archive/$NJS_VERSION.tar.gz | tar -xzf - && \
    curl --location --retry 3 http://ffmpeg.org/releases/ffmpeg-$FFMPEG_VERSION.tar.gz | tar -xzf - && \
    apk del .download

RUN printf "${REPO_MIRRORS_URL}/%s\n" \
    v${ALPINE_VERSION%.*}/main \
    v${ALPINE_VERSION%.*}/community \
    > /etc/apk/repositories && \
    apk update --no-cache; \
:                \
:   install apk  \
;                \
    apk add --no-cache --virtual .build-nginx \
    build-base \
    gd-dev \
    geoip-dev \
    libxslt-dev \
    linux-headers \
    openssl-dev \
    pcre-dev \
    zlib-dev; \
:                \
:   build nginx  \
;                \
    find /bin /etc /lib /run /sbin /usr /var ! -type d | sort > /tmp/before-0.log; \
    cd /tmp/nginx-$NGINX_VERSION && \
    ./configure \
    --add-module=/tmp/nginx-rtmp-module-$NGINX_RTMP_VERSION \
    --add-dynamic-module=/tmp/njs-$NJS_VERSION/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --lock-path=/var/run/nginx.lock \
    --modules-path=/usr/lib/nginx/modules \
    --pid-path=/var/run/nginx.pid \
    --prefix=/etc/nginx \
    --sbin-path=/usr/sbin/nginx \
    --http-client-body-temp-path=/var/cache/nginx/client_temp \
    --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
    --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
    --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
    --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
    --with-perl_modules_path=/usr/lib/perl5/vendor_perl \
    --user=nginx \
    --group=nginx \
    --with-compat \
    --with-file-aio \
    --with-http_addition_module \
    --with-http_auth_request_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_geoip_module=dynamic \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_image_filter_module=dynamic \
    --with-http_mp4_module \
    --with-http_random_index_module \
    --with-http_realip_module \
    --with-http_secure_link_module \
    --with-http_slice_module \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_sub_module \
    --with-http_v2_module \
    --with-http_xslt_module=dynamic \
    --with-mail \
    --with-mail_ssl_module \
    --with-stream \
    --with-stream_realip_module \
    --with-stream_ssl_module \
    --with-stream_ssl_preread_module \
    --with-stream_geoip_module=dynamic \
    --with-threads \
    --with-cc-opt='-Os -fomit-frame-pointer' \
    --with-ld-opt=-Wl,--as-needed && \
    make CFLAGS='-Wno-implicit-fallthrough' && \
    make install; \
    rm -fv /etc/nginx/*.default; \
    mkdir -v /etc/nginx/conf.d /usr/share/nginx; \
    mv -v /etc/nginx/html /usr/share/nginx/ && \
    cd /etc/nginx/ && \
    printf "%s\n" \
H4sIAAAAAAAAA+1Y64/TO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9kBkBlfcjhwNHo6RUM/0MZi7nkmH0q7SjVkt7KkS/ToGIKpmUiyiCXoGXKcx3a75lA4 \
kQXPtrzuODT6dw3HpUJK5IrF6NsTdP763dn5q7df3705ff5ib5iRP9xxphYYES7pUg0XBHlERh4w \
vYRIKPSI3+bSU2nzqrPxlkWSRCtoum5O0sV+R+Ga3H6Xs5zTK9A1rkzXj+NwRXAMpfFn/yWHxPTP \
czPivT39/cX5m7Nfz16Vxwzr4HDj0JrRWZ/bxRyYG1RMtz48KMioZig76Xm1MU+BN3x5heCegGCk \
dCTTZDPf0SwmN/bLVaLN48KOfmVRlkG+oY+webKkN/1HAnAPvoOv9mHw2Gsm0C4ixhIvanw1Y5vQ \
1bTZ3RPq4zcwtde96digKxzg5sTfEJz0z16jOg4uDpu8LKc39YSOjYZK49Zgd9DNb5AZhDaX6p6Y \
r1QW+q+VAElekIPqz6vXBLiCu5o5FkJfQLh/NVg8fKDX5EIwgF/ZnuhBzT/ySw4Pdh2F+bVroaWA \
xEK5DNzgu6Nbj/ektdHS/Lpu12pzElN4dZOlRYXI22VQawkAqSPUIA84rsEGa1b5te1OmyNlLpBk \
SK4IUh2aRpqHvLFvEMSY67/1iDBh+fAZwGcIHwhfWSyaKPZsI/lXeFaVe0yyW/sOplYKnuwPNYiJ \
E9WITnMcrUhPwDQRFal6HVT+rT28GEQFDHrXVK7M/xOAIstIbWfdGhx6f0GE+l3S8RFO7Lq6d//b \
V62WWmqppZZaaqmlllpqqaWWWmqppZZaaqmlH0r/AOOO1yoAKAAA \
| base64 -d | tar -xzvf - && chmod 644 *.conf && \
    mv -v /etc/nginx/default.conf /etc/nginx/conf.d/; \
    find /bin /etc /lib /run /sbin /usr /var ! -type d | sort > /tmp/after-0.log; \
    apk del .build-nginx; \
:                              \
:   manipulate archive files   \
;                              \
    mkdir -pv /tmp/rootfs; \
    diff /tmp/before-0.log /tmp/after-0.log | \
    awk '/^\+\// && !/\.(a|c|h|log)$|examples/{sub(/\+/, "", $0); print $0}' | \
    cpio -d -p /tmp/rootfs;


# ffmpeg
RUN printf "${REPO_MIRRORS_URL}/%s\n" \
    edge/community \
    >> /etc/apk/repositories && \
    apk update --no-cache; \
:               \
:   update apk  \
;               \
    apk add --update --no-cache \
    build-base \
    freetype-dev \
    lame-dev \
    libass-dev \
    libtheora-dev \
    libvorbis-dev \
    libvpx-dev \
    libwebp-dev \
    nasm \
    openssl-dev \
    opus-dev \
    rtmpdump-dev \
    x264-dev \
    x265-dev \
    fdk-aac-dev; \
:                 \
:   build ffmpeg  \
;                 \
    find /bin /etc /lib /run /sbin /usr /var ! -type d | sort > /tmp/before-1.log; \
    cd /tmp/ffmpeg-$FFMPEG_VERSION && \
    ./configure \
    --disable-debug \
    --disable-doc \
    --disable-ffplay \
    --disable-static \
    --enable-avresample \
    --enable-gpl \
    --enable-libass \
    --enable-libfdk-aac \
    --enable-libfreetype \
    --enable-libmp3lame \
    --enable-libopus \
    --enable-librtmp \
    --enable-libtheora \
    --enable-libvorbis \
    --enable-libvpx \
    --enable-libwebp \
    --enable-libx264 \
    --enable-libx265 \
    --enable-nonfree \
    --enable-openssl \
    --enable-postproc \
    --enable-shared \
    --enable-small \
    --enable-version3 && \
    make && \
    make install; \
    find /bin /etc /lib /run /sbin /usr /var ! -type d | sort > /tmp/after-1.log; \
:                              \
:   manipulate archive files   \
;                              \
    mkdir -pv /tmp/rootfs; \
    diff /tmp/before-1.log /tmp/after-1.log | \
    awk '/^\+\// && !/\.(a|c|h|log)$|examples/{sub(/\+/, "", $0); print $0}' | \
    cpio -d -p /tmp/rootfs;



FROM alpine:$ALPINE_VERSION

LABEL maintainer="binave <nidnil@icloud.com>"

ARG ALPINE_VERSION
ARG REPO_MIRRORS_URL

RUN printf "${REPO_MIRRORS_URL}/%s\n" \
    v${ALPINE_VERSION%.*}/main \
    v${ALPINE_VERSION%.*}/community \
    edge/community \
    > /etc/apk/repositories && \
    apk update --no-cache; \
    apk add --no-cache \
    fdk-aac \
    geoip \
    lame \
    libass \
    libedit \
    libgcrypt \
    libgd \
    libintl \
    libtheora \
    libvorbis \
    libvpx \
    libwebp \
    libxml2 \
    nghttp2-libs \
    opus \
    pcre \
    rtmpdump \
    tzdata \
    x264-libs \
    x265-libs; \
:            \
:    user    \
;            \
    addgroup -g 101 -S nginx && \
    adduser -S -D -u 101 -h /var/cache/nginx -s /sbin/nologin -G nginx -g nginx nginx; \
    mkdir -pv /var/log/nginx /run/nginx; \
    ln -fsv /dev/stdout /var/log/nginx/access.log && \
    ln -fsv /dev/stderr /var/log/nginx/error.log;

COPY --from=build /tmp/rootfs /

EXPOSE 80
EXPOSE 443
EXPOSE 1935

STOPSIGNAL SIGTERM

CMD ["nginx", "-g", "daemon off;"]
